//
//  HomeViewCell.swift
//  JMWB
//
//  Created by 个人电脑 on 16/3/22.
//  Copyright © 2016年 个人电脑. All rights reserved.
//

import UIKit


class HomeViewCell: UITableViewCell {
    //头像
    @IBOutlet weak var iconImageView: UIImageView!
    //认证
    @IBOutlet weak var verifiedImageView: UIImageView!
    //会员
    @IBOutlet weak var vipImageView: UIImageView!
    //昵称
    @IBOutlet weak var screenNameLabel: UILabel!
    //发布时间
    @IBOutlet weak var timeLabel: UILabel!
    //来源
    @IBOutlet weak var sourceLabel: UILabel!
    //正文
    @IBOutlet weak var contentLabel: UILabel!
    //底部工具栏
    @IBOutlet weak var bottomToolView: UIView!
    //转发微博
    @IBOutlet weak var retweetedContentLabel: UILabel!
    //图片控制器
    @IBOutlet weak var picCollectionView: PicCollectionView!
    //转发背景颜色
    @IBOutlet weak var retweetedBgView: UIView!
    
  
  //正文宽度约束
    @IBOutlet weak var contentLabelWidthCons: NSLayoutConstraint!

  //转发微博和正文的间距约束
    @IBOutlet weak var retweetedLabelTopCons: NSLayoutConstraint!
   //图片顶部的约束
    @IBOutlet weak var picCollectionViewTopCons: NSLayoutConstraint!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //微博正文的文字宽度约束
        contentLabelWidthCons.constant = UIScreen.mainScreen().bounds.width - 2 * margin
        
    }
    var statusViewModel : StatusViewModel?{
        didSet{
            guard let statusViewModel = statusViewModel else{
                return
            }
            iconImageView.sd_setImageWithURL(statusViewModel.iconURL, placeholderImage: UIImage(named: "avatar_default"))
            
            verifiedImageView.image = statusViewModel.verifiedImage
            
            vipImageView.image = statusViewModel.vipImage
            
            screenNameLabel.text = statusViewModel.status?.user?.screen_name
            
            timeLabel.text = statusViewModel.createAtString
            
            sourceLabel.text = statusViewModel.sourceText
            
            contentLabel.text = statusViewModel.status?.text
            
                        
            //设置picCollectionView的picURLs
           picCollectionView.picURLs = statusViewModel.picURLs
            
            //设置转发微博的内容
            if statusViewModel.status?.retweeted_status != nil{
            // 1.设置转发微博的正文
                retweetedContentLabel.text = "@" + (statusViewModel.status?.retweeted_status?.user?.screen_name)! + ": " + (statusViewModel.status?.retweeted_status?.text ?? "")
            // 2.修改转发微博的Label距离顶部的约束
             retweetedLabelTopCons.constant = 15
            //设置转发背景颜色
            retweetedBgView.hidden = false
                
            }else{
            retweetedContentLabel.text = nil
            retweetedLabelTopCons.constant = 0
            retweetedBgView.hidden = true
                
            }
            //判断是否有图片
//            if statusViewModel.picURLs.count == 0{
//            picCollectionViewTopCons.constant = 0
//            }else{
//            picCollectionViewTopCons.constant = 10
//            }
            //判断是否有图片
            picCollectionViewTopCons.constant = statusViewModel.picURLs.count == 0 ? 0 : 10
        }
    }
}
extension HomeViewCell{
        //计算cell的高度,为避免约束冲突,将系统自动计算cell的高度改为自己计算,将工具栏距离底部的约束删除点
    func cellHeight(statusViewModel : StatusViewModel) -> CGFloat{
        self.statusViewModel = statusViewModel
        //更新所有的约束
        self.layoutIfNeeded()
        //拿到底部工具栏最大的Y值
        return CGRectGetMaxY(bottomToolView.frame)
    }
}